package com.primaryedu.adapter;

import java.util.ArrayList;
import java.util.List;

import com.bumptech.glide.Glide;
import com.primaryedu.R;
import com.primaryedu.bean.NewsBean;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 新闻资讯
 *
 * @author jun.wang
 * @description
 * @data 2016年12月13日
 */
public class NewsAdapter extends BaseAdapter {
    private Context context;
    LayoutInflater inflater;
    private List<NewsBean> newsLists = new ArrayList<>();
//    LoadImage loadImage;

    @SuppressWarnings("static-access")
    public NewsAdapter(Context context, List<NewsBean> newsLists) {
        this.context = context;
        this.newsLists = newsLists;
//        loadImage = LoadImage.getInstance();
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return newsLists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_news, null);
            viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
            viewHolder.title = (TextView) convertView.findViewById(R.id.text_title);
            viewHolder.content = (TextView) convertView.findViewById(R.id.text_content);
            viewHolder.image_player = (ImageView) convertView.findViewById(R.id.image_player);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        NewsBean bean = newsLists.get(position);
//        viewHolder.image.setTag(bean.getPicurl());
        Glide.with(context)
                .load(bean.getPicurl())
                .placeholder(R.drawable.default_error)
                .error(R.drawable.default_error)
                .centerCrop()
                .crossFade()
                .into(viewHolder.image);
//        loadImage.addTask(bean.getPicurl(), viewHolder.image);
//        loadImage.doTask();

        viewHolder.title.setText(bean.getTitle());
        viewHolder.content.setText(bean.getContent());
        viewHolder.image_player.setVisibility(View.GONE);

        return convertView;
    }

    class ViewHolder {
        ImageView image;
        TextView title;
        TextView content;
        ImageView image_player;
    }

}
